home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Utilities / Text and Speech / BBEdit 2.2.2 / BBEdit Extensions / Sources / CapSentence.c < prev    next >
C/C++ Source or Header  |  1992-10-09  |  2KB  |  88 lines

  1. #include "ExternalInterface.h"
  2.  
  3. static long get_handle_size(Handle h)
  4. {
  5.     asm {
  6.         movea.l    h, a0
  7.         _GetHandleSize
  8.     }
  9. }
  10.  
  11. static void *strip_address(void *p)
  12. {
  13.     asm {
  14.         move.l    p, d0
  15.         _StripAddress
  16.     }
  17. }
  18.  
  19. #define SENTENCE_END(c) ((c == '.') || (c == '!') || (c == '?'))
  20. #define WHITE_SPACE(c) ((c == 0x09) || (c == 0x0D) || (c == ' '))
  21.  
  22. static Boolean CapitalizeSentences( unsigned char *p, long start, long end )
  23. {
  24.     long i, j;
  25.     Boolean result = FALSE;
  26.     
  27.     /*    The first non-whitespace character should always be capitalized. */
  28.     j = start;
  29.     while (WHITE_SPACE(p[j]) && (j < end))
  30.         j++;
  31.     
  32.     i = j;
  33.     p[j] &= 0xDF;
  34.     
  35.     /*    Now do the rest... */
  36.     
  37.     while ( i < end )
  38.     {
  39.         while (! SENTENCE_END(p[i]) && (i < end))
  40.             i++;
  41.             
  42.         if (i == end)
  43.             break;
  44.             
  45.         if ((p[++i] == ' ') || (p[i] == 0x09) || (p[i] == 0x0D)) {
  46.                     
  47.             while ((i < end) && WHITE_SPACE(p[i]))
  48.                 i++;
  49.         
  50.             if (i == end)
  51.                 break;
  52.                 
  53.             if ((p[i] >= 'a') && (p[i] <= 'z')) {
  54.                 p[i] &= 0xDF;
  55.                 result = TRUE;
  56.             };
  57.         };
  58.     }
  59.     
  60.     return result;
  61. }
  62.  
  63.  
  64. pascal void main( ExternalCallbackBlock *callbacks, WindowPtr w )
  65. {
  66.     Handle text;
  67.     Size size;
  68.     long selStart, selEnd, firstChar;
  69.     
  70.     if (! w)
  71.         return;
  72.         
  73.     if ( callbacks->version >= 2 ) {    
  74.         text = callbacks->GetWindowContents( w );
  75.         size = get_handle_size(text);
  76.         callbacks->GetSelection(&selStart, &selEnd, &firstChar);
  77.         
  78.         if (selStart == selEnd) {
  79.             selStart = 0;
  80.             selEnd = size;
  81.         }
  82.         
  83.         if (CapitalizeSentences((unsigned char *)*text, (Size) selStart, (Size) selEnd))
  84.             callbacks->ContentsChanged( w );
  85.         
  86.     };
  87. }
  88.